package com.estronger.shopmanage.ui.othertask.presenter;

import com.estronger.shopmanage.ui.othertask.activity.OtherTaskMainActivity;
import com.estronger.shopmanage.ui.othertask.view.IUpdateOtherTaskResultPresenter;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.soonfor.evaluate.http.api.Request;
import com.soonfor.evaluate.http.httptools.AsyncUtils;
import com.soonfor.evaluate.tools.CommonUtils;
import com.estronger.shopmanage.ui.othertask.activity.UpdateOtherTaskResultActivity;
import com.soonfor.evaluate.bean.ReturnVisitBean;

/**
 * Created by ljc on 2018/1/25.
 */

public class UpdateOtherTaskResultPresenter implements IUpdateOtherTaskResultPresenter, AsyncUtils.AsyncCallback {

    private int listpositon;
    private UpdateOtherTaskResultActivity view;

    public UpdateOtherTaskResultPresenter(UpdateOtherTaskResultActivity view) {
        this.view = view;
    }

    //获取详情信息
//    @Override
//    public void getDetailInfo(int taskType, String json) {
//        AsyncUtils.OtherTask.getOtherTaskDetailInfo(taskType, view, json, this);
//    }

    @Override
    public void setListpostion(int listpos) {
        this.listpositon = listpositon;
    }

    //更新任务结果
    @Override
    public void updateTaskResult(Map<String, String> params) {
        Request.OtherTask.updateTaskResult(view, params, this);
    }

    //获取回访题目
    @Override
    public void getReturnVisitPrj(String fproid){
        Request.OtherTask.getReturnVisitPrj(view, fproid, this);
    }


    @Override
    public void success(int requestCode, String data) {
        view.mLoadingDialog.dismiss();
        Gson gson = new Gson();
        switch (requestCode) {
            case Request.OtherTask.GET_RETURNVISIT_PRJ://获取回访题目
                List<ReturnVisitBean> rvists = new ArrayList<>();
                try {
                    JSONArray ja = new JSONArray(data);
                    if(ja!=null && ja.length()>0){
                        for(int i=0; i<ja.length(); i++){
                            ReturnVisitBean visitBean = gson.fromJson(ja.getJSONObject(i).toString(), ReturnVisitBean.class);
                            if(visitBean.getFtype()==1){//是否题
                                List<ReturnVisitBean.Answer> answers = new ArrayList<>();
                                ReturnVisitBean.Answer answer = new ReturnVisitBean.Answer("是", visitBean.getFpoint(), 0);
                                answers.add(answer);
                                answer = new ReturnVisitBean.Answer("否", visitBean.getFpoint(), 0);
                                answers.add(answer);
                                visitBean.setItem(answers);
                            }
                            rvists.add(visitBean);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                view.setGetReturnVisit(true, rvists);
                break;
            case Request.OtherTask.UPDATE_TASK_RESULT:
                view.mLoadingDialog.dismiss();
                if (view.getButtonType() == 1) {
                    view.goToProfile();
                } else {//if(view.getButtonType()==2){
                    try {
                        JSONObject js = new JSONObject(data);
                        view.geToEvaluateCustomer(CommonUtils.formatStr(js.getString("fappraiseoriid")),
                                CommonUtils.formatStr(js.getString("fappraiseoritype")));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                OtherTaskMainActivity.ItemPosition = listpositon;
                view.finish();
                break;
        }
    }

    @Override
    public void fail(int requestCode, int statusCode, String data, String msg) {
        view.mLoadingDialog.dismiss();
        switch (requestCode) {
            case Request.OtherTask.GET_RETURNVISIT_PRJ:
                view.setGetReturnVisit(false, null);
                break;
            case Request.OtherTask.UPDATE_TASK_RESULT:
               view.showTrop("保存失败！");
                break;
        }
    }
}
